/*
 * BottomBar.java
 * business
 *
 * Created by ChenTao on 2017/2/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.support.annotation.IntDef;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.model.ConfigModel;
import com.yidiandao.business.utils.LogUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 首页底部栏.
 */
public class BottomBar extends LinearLayout implements View.OnClickListener {

    public static final int HOME = 0;
    public static final int CATEGORY = 1;
    public static final int DISCOVERY = 2;
    public static final int SHOPPING_CART = 3;
    public static final int USER_CENTER = 4;

    @IntDef(
            {HOME, CATEGORY, DISCOVERY, SHOPPING_CART, USER_CENTER}
    )
    @Retention(RetentionPolicy.SOURCE)
    public @interface Position {
    }

    private RelativeLayout mRlytHome;
    private RelativeLayout mRlytCategory;
    private RelativeLayout mRlytDiscovery;
    private RelativeLayout mRlytShoppingTrolley;
    private RelativeLayout mRlytUserCenter;
    private ImageView mIvHome;
    private TextView mTvHome;
    private ImageView mIvCategory;
    private TextView mTvCategory;
    private ImageView mIvDiscovery;
    private TextView mTvDiscovery;
    private ImageView mIvShoppingCart;
    private TextView mTvShoppingCart;
    private ImageView mIvUserCenter;
    private TextView mTvUserCenter;
    private OnBottomBarItemClickListener mListener;
    private int mCurrentPosition;

    public BottomBar(Context context) {
        this(context, null);
    }

    public BottomBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        initViews(context);
    }

    private void initViews(Context context) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.bottom_bar, this, true);
        mRlytHome = (RelativeLayout) rootView.findViewById(R.id.rlyt_home);
        mRlytCategory = (RelativeLayout) rootView.findViewById(R.id.rlyt_category);
        mRlytDiscovery = (RelativeLayout) rootView.findViewById(R.id.rlyt_discovery);
        mRlytShoppingTrolley = (RelativeLayout) rootView.findViewById(R.id.rlyt_shopping_cart);
        mRlytUserCenter = (RelativeLayout) rootView.findViewById(R.id.rlyt_user_center);

        mIvHome = (ImageView) rootView.findViewById(R.id.iv_home);
        mTvHome = (TextView) rootView.findViewById(R.id.tv_home);
        mIvCategory = (ImageView) rootView.findViewById(R.id.iv_category);
        mTvCategory = (TextView) rootView.findViewById(R.id.tv_category);
        mIvDiscovery = (ImageView) rootView.findViewById(R.id.iv_discovery);
        mTvDiscovery = (TextView) rootView.findViewById(R.id.tv_discovery);
        mIvShoppingCart = (ImageView) rootView.findViewById(R.id.ic_shopping_cart);
        mTvShoppingCart = (TextView) rootView.findViewById(R.id.tv_shopping_cart);
        mIvUserCenter = (ImageView) rootView.findViewById(R.id.iv_user_center);
        mTvUserCenter = (TextView) rootView.findViewById(R.id.tv_user_center);

        mRlytHome.setOnClickListener(this);
        mRlytCategory.setOnClickListener(this);
        mRlytDiscovery.setOnClickListener(this);
        mRlytShoppingTrolley.setOnClickListener(this);
        mRlytUserCenter.setOnClickListener(this);

        // 默认选择首页
        setSelected(HOME);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rlyt_home:
                if (mListener != null) {
                    if (mListener.onBottomBarItemClick(HOME)) {
                        setSelected(HOME);
                    }
                }
                break;
            case R.id.rlyt_category:
                if (mListener != null) {
                    if (mListener.onBottomBarItemClick(CATEGORY)) {
                        setSelected(CATEGORY);
                    }
                }
                break;
            case R.id.rlyt_discovery:
                if (mListener != null) {
                    if (mListener.onBottomBarItemClick(DISCOVERY)) {
                        setSelected(DISCOVERY);
                    }
                }
                break;
            case R.id.rlyt_shopping_cart:
                if (mListener != null) {
                    if (mListener.onBottomBarItemClick(SHOPPING_CART)) {
                        setSelected(SHOPPING_CART);
                    }
                }
                break;
            case R.id.rlyt_user_center:
                if (mListener != null) {
                    if (mListener.onBottomBarItemClick(USER_CENTER)) {
                        setSelected(USER_CENTER);
                    }
                }
                break;
        }
    }

    public void setSelected(@Position int position) {
        mCurrentPosition = position;
        // 首页
        mIvHome.setSelected(position == HOME);
        mTvHome.setSelected(position == HOME);

        // 分类
        mIvCategory.setSelected(position == CATEGORY);
        mTvCategory.setSelected(position == CATEGORY);

        // 发现
        mIvDiscovery.setSelected(position == DISCOVERY);
        mTvDiscovery.setSelected(position == DISCOVERY);

        // 购物车
        mIvShoppingCart.setSelected(position == SHOPPING_CART);
        mTvShoppingCart.setSelected(position == SHOPPING_CART);

        // 用户中心
        mIvUserCenter.setSelected(position == USER_CENTER);
        mTvUserCenter.setSelected(position == USER_CENTER);
    }

    /**
     * 获取当前的Tab
     */
    @Position
    public int getCurrentPosition() {
        return mCurrentPosition;
    }

    /**
     * 更新BottomBar 图片
     */
    public void updateBottomBarStateImages() {
        try {
            LogUtils.d("更新BottomBar---DOWNLOAD");
            updateTabStateImageWithDownloadImage(mIvHome, "home");
            updateTabStateImageWithDownloadImage(mIvCategory, "category");
            updateTabStateImageWithDownloadImage(mIvDiscovery, "discovery");
            updateTabStateImageWithDownloadImage(mIvShoppingCart, "shopping_cart");
            updateTabStateImageWithDownloadImage(mIvUserCenter, "user_center");
        } catch (Exception e) {
            LogUtils.d("更新BottomBar---原来的");

            mIvHome.setBackgroundDrawable(getResources().getDrawable(R.drawable.sl_ic_home));
            mIvCategory.setBackgroundDrawable(getResources().getDrawable(R.drawable.sl_ic_category));
            mIvDiscovery.setBackgroundDrawable(getResources().getDrawable(R.drawable.sl_ic_discovery));
            mIvShoppingCart.setBackgroundDrawable(getResources().getDrawable(R.drawable.sl_ic_shopping_cart));
            mIvUserCenter.setBackgroundDrawable(getResources().getDrawable(R.drawable.sl_ic_user_center));
        }
    }

    private void updateTabStateImageWithDownloadImage(ImageView imageView, String tabName)
            throws Exception {
        String absoluteDir = ConfigModel.BOTTOM_MENU_DIR_NAME
                + File.separator
                + ConfigModel.BOTTOM_MENU_UNZIP_DIR_NAME
                + File.separator
                + ConfigModel.ICON_DIR;
        File normalImageFile = new File(absoluteDir, "ic_" + tabName + "_normal@3x.png");
        File clickImageFile = new File(absoluteDir, "ic_" + tabName + "_click@3x.png");

        if (!normalImageFile.exists() || normalImageFile.length() == 0) {
            throw new FileNotFoundException("更新包中的图片文件未找到");
        }
        if (!clickImageFile.exists() || clickImageFile.length() == 0) {
            throw new FileNotFoundException("更新包中的图片文件未找到");
        }

        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(
                new int[]{android.R.attr.state_selected},
                Drawable.createFromPath(clickImageFile.getAbsolutePath()));
        stateListDrawable.addState(
                new int[0], Drawable.createFromPath(normalImageFile.getAbsolutePath()));
        imageView.setBackgroundDrawable(stateListDrawable);
    }

    public void setOnBottomBarItemClickListener(OnBottomBarItemClickListener listener) {
        mListener = listener;
    }

    /**
     * 底部栏点击的回掉.
     */
    public interface OnBottomBarItemClickListener {

        /**
         * BottomBar Item点击的回掉
         *
         * @param POSITION 点击的position
         * @return true:UI变化 ; false:UI不变
         */
        boolean onBottomBarItemClick(@Position int POSITION);
    }
}
